<?php

function tmPostSellBuyRealestate(){ 
    if(!user_is_logged_in()){//Nếu chưa đăng nhập
        drupal_goto();
    }
    drupal_set_title('Đăng tin rao vặt nhà đất');
    //add css
    drupal_add_css(drupal_get_path('module', 'tbreal') . '/assets/css/post_realestate.css');
    drupal_add_css(drupal_get_path('module', 'tbnd_plugin') . '/assets/css/jquery.qtip.css');    
    drupal_add_css(drupal_get_path('module', 'tbreal') . '/assets/css/manager_account.css');
    drupal_add_css(drupal_get_path('module', 'tbnd_plugin') . '/upload/css/fileuploader.css');
    drupal_add_css(drupal_get_path('module', 'tbnd_plugin') . '/upload/css/custom_fileuploader.css');
   
    //add js
    drupal_add_js(drupal_get_path('module', 'tbreal') . '/assets/js/tbnd_create_product.js');
    drupal_add_js("http://maps.googleapis.com/maps/api/js?key=AIzaSyA0IQWz23j9eIy8DRS9h2HNq2wpuM1shhQ&sensor=true&language=viet", 'external');
    //add js plugin
    drupal_add_js(drupal_get_path('module', 'tbnd_plugin') . '/assets/js/jquery.qtip-1.0.0-rc3.js');
    drupal_add_js(drupal_get_path('module', 'tbnd_plugin') . '/numeric/js/jquery.numeric.js');
    //drupal_add_js(drupal_get_path('module', 'tbnd_plugin') . '/numeric/js/jquery.numberformatter-1.2.3.js');
    drupal_add_js(drupal_get_path('module', 'tbnd_plugin') . '/upload/js/fileuploader.js');
    
    $data = array();
    
    //$data['aInfoReal'] = getInfoBasicRealEstate();
    global $user; 
    $data['user'] = $user;
    $data['form_post_realestate'] = theme('form_post_realestate' , $data);
    return theme('page_post_realestate' , $data);
}

function pageRealestate(){ 	
	//add css
    drupal_add_css(drupal_get_path('module', 'tbreal') . '/assets/css/product_realestate.css');    
    drupal_add_css(drupal_get_path('module', 'tbreal') . '/assets/css/filter_product_realestate.css');
    
     //add js
    drupal_add_js(drupal_get_path('module', 'tbreal') . '/assets/js/filter_product_realestate.js' , 'external');
    drupal_add_js(drupal_get_path('module', 'tbreal') . '/assets/js/lib_realestate.js');
    
	$result = mdGetAllListRealEstate( $start = 0 , $limit = 20); 
	$data['list_real'] = theme('list_real', array('rows' => $result['data'])); 
	    
    $data['block_search_realestate'] = theme('block_search_realestate' , array('s' => $_GET));
    
    $data['breadcrumb'] = theme('tbnd_breadcrumb');		
    /*------------remove_after_finish_---------------------*/
    $list_product_vip = mdGetListRealVipByParam(0 , 10);
    $data['adv_box_right_300_product_realestate'] = theme('adv_box_right_300_product_realestate' , array('list_vip' => $list_product_vip));
    /*---------------------------------------------------*/
    
	return theme('page_realestate' , $data);
}

function pageDetailRealestate(){
	//add css
    drupal_add_css(drupal_get_path('module', 'tbreal') . '/assets/css/product_realestate.css');    
    drupal_add_css(drupal_get_path('module', 'tbreal') . '/assets/css/filter_product_realestate.css');
    
    //add js
    drupal_add_js("http://maps.googleapis.com/maps/api/js?key=AIzaSyA0IQWz23j9eIy8DRS9h2HNq2wpuM1shhQ&sensor=true&language=viet", 'external');
    drupal_add_js(drupal_get_path('module', 'tbreal') . '/assets/js/filter_product_realestate.js' , 'external');
    drupal_add_js(drupal_get_path('module', 'tbreal') . '/assets/js/detail_realestate.js');
	$rid = arg( 1 );
	if($rid > 0){
		$row = mdGetInfoRealEstateById( $rid ); 
		if($row){
			//*---------------and_canonical-----------------------*/
			$canonical = array(
				  '#type' => 'html_tag',
				  '#tag' => 'link',
				  '#attributes' => array(
					  //'name' => '',
					  'ref' => 'canonical',
					  'href' => $row->link,
				  ),
			  );
			  //  add the canonical object to the html head
			  drupal_add_html_head($canonical,'canonical');
			//*---------------and_canonical-----------------------*/ 
			
			/*------------------------add_meta_tag------------------*/
			addMetaTag('title' , $row->title . ' - ' . $row->rid . ' | thuebannhadat.vn');			
			addMetaTag('description', api_cut_words(trim($row->content), 160 , $word_cut = '', $charset = 'UTF-8'));
			/*------------------------and_meta_tag------------------*/
			/*------------------------add_og_meta_tag------------------*/
			addOgMetaTag('title' , $row->title);	
			addOgMetaTag('description', api_cut_words(trim($row->content), 160 , $word_cut = '', $charset = 'UTF-8'));	
			addOgMetaTag('image' , $row->main_image->thubm);			
			/*------------------------add_og_meta_tag------------------*/			
			$data['row'] = $row;
            $n_type_post = mdGetTypePost( $row->type_post );
            $n_type_real = mdGetTypeRealById( $row->type_real )->name;
            $title = $n_type_real . ' tại ';            
            $s = 'tim-kiem-nha-dat?';
            /*$s .= 'slb_type_post=' . $row->type_post;
            $s .= '&slb_type_real=' . $row->type_real;
            
            $s .= '&slb_district=' . $row->id_district;
            $s .= '&slb_city=' . $row->id_city;       
                1 =>  array(
                    'index' => 1 , 
                    'name' =>  $n_type_post,
                    'href' => BASE_URL . $s 
                ),
                2 => array(
                    'index' => 2, 
                    'name' => $n_type_real, 
                    'href' => BASE_URL . $s 
                ),
                3 =>  array(
                    'index' => 3 , 
                    'name' => $row->name_city , 
                   'href' => BASE_URL . $s 
                ),
                4 =>  array(
                    'index' => 4 , 
                    'name' => $row->name_district , 
					'href' => BASE_URL . $s 
                ),
            );      */    
            $breadcrumb =  array(); 
            
            if($row->type_post){			
                $s .= 'slb_type_post=' . $row->type_post;
                $breadcrumb[1] = array(
                    'index' => 1 , 
                    'name' =>  $n_type_post,
                    'href' => BASE_URL . $s 
                );
               
            }          
            if($row->type_real){			
                $s .= '&slb_type_real=' . $row->type_real;
                $breadcrumb[2] = array(
                    'index' => 2 , 
                    'name' =>  $n_type_real,
                    'href' => BASE_URL . $s 
                );
               
            }      
            if($row->id_city){				
                $s .= '&slb_city=' . $row->id_city;
                $breadcrumb[3] = array(
                    'index' => 3 , 
                    'name' =>  $row->name_city,
                    'href' => BASE_URL . $s 
                );
               
            }        
            if($row->id_district){				
                $s .= '&slb_district=' . $row->id_district;
                $breadcrumb[4] = array(
                    'index' => 4 , 
                    'name' =>  $row->name_district,
                    'href' => BASE_URL . $s 
                );
               
            }          
            if($row->project){
				$title .= ' trong dự án ' . $row->name_project;
                $s .= '&slb_project=' . $row->project;
                $breadcrumb[7] = array(
                    'index' => 7 , 
                    'name' => $row->name_project , 
                    'href' => BASE_URL . $s 
                );
               
            }            
            if($row->id_street){
				 $title .= ' đường ' . $row->name_street;
                $s .= '&slb_street=' . $row->id_street;
                $breadcrumb[6] = array(
                    'index' => 6 , 
                    'name' => $row->name_street , 
                    'href' => BASE_URL . $s 
                );               
            }
            if($row->id_ward){
				$title .= ' phường ' . $row->name_ward;
                $s .= '&slb_ward=' . $row->id_ward;
                $breadcrumb[5] = array(
                    'index' => 5 , 
                    'name' => $row->name_ward , 
                    'href' => BASE_URL . $s 
                );
               
            }
          
            $title .= ' ' . $row->name_district;
            
            $aS = array(
                'slb_type_post' => $row->type_post ,
                'slb_type_real' => $row->type_real ,
                'slb_city' => $row->id_city ,
                'slb_district' => $row->id_district ,
                'slb_ward' => $row->id_ward ,
                'slb_street' => $row->id_street ,
                'slb_project' => $row->project ,
            );
            /*--------------add_key_words----------------------*/
            $keyword = str_replace(',' , '', trim($title));
			addMetaTag('keywords', str_replace(' ' , ', ', trim(api_cut_words($keyword , 50, $word_cut = '', $charset = 'UTF-8'))));            
            /*-------------------------------------------------*/
            
            $list_statistic_price = mdTotalProductByPrice( $aS ); 
            $list_statistic_area = mdTotalProductByArea( $aS ); 
            $data['block_statistic_product_price'] = theme('block_statistic_product_price' , array('n_type_real' => $n_type_real , 'title' => $title , 'list_statistic' => $list_statistic_price , 'kS' => $s));
            $data['block_statistic_product_area'] = theme('block_statistic_product_area' , array('title' => $result['title'] , 'list_statistic' => $result['list_statistic_area']));
            
            $data['block_search_realestate'] = theme('block_search_realestate' , array('s' => $aS));
			$data['breadcrumb'] = theme('tbnd_breadcrumb' , array('links' => $breadcrumb));	
			//$list_product_vip = mdGetListRealVipByParam(0 , 10);
			//$data['adv_box_right_300_product_realestate'] = theme('adv_box_right_300_product_realestate' , array('list_vip' => $list_product_vip));
			
			//$result = mdSearchRealestate($aS , $start = 0 , $limit = ITEM_PER_PAGE_10);   
			$result = mdSearchRelationshipDetailRealestate($rid , $aS);   
			$data['list_real'] = theme('list_real', array('rows' => $result));       
			$data['title'] = $title;
			
			return theme('page_detail_product_realestate' , $data);
		}
	}
	
}

function import_data_base(){
	db_set_active('tibi');
	$sql = 'SELECT * FROM tb_realestate_ad  WHERE activation = 1 AND 1 = 1';  
	$rows = db_query_range( $sql, $start = 500 , $limit = 1000)->fetchAll();	

	foreach($rows as $row){ 
		//die(print_r(strip_tags($row->content)));
		$data = array(
			'pr_txt_title' => $row->title , 
			'pr_tar_decription' => strip_tags($row->content) ,
			'pr_slb_type_post' => $row->id_type_news ,
			'pr_slb_type_real' => $row->id_type_realestate , 
			'pr_slb_city' => $row->id_city , 
			'pr_slb_district' => $row->id_district ,
			'pr_slb_ward' => $row->id_ward ,
			'pr_slb_street' => $row->id_street ,
			'pr_full_address' => $row->full_address ,
			'pr_txt_latitude' => $row->latitude ,
			'pr_txt_longitude' => $row->longitude ,
			'pr_txt_area' => $row->area ,
			'pr_txt_total_price' => $row->price ,
			'pr_txt_text_price' => $row->text_price , 
			'pr_slb_legal' => $row->phap_ly ,
			'pr_slb_direction' => $row->huong ,
			'pr_txt_floor' => $row->so_lau ,
			'pr_txt_toilet' =>$row->so_phong_tam ,
			
			'txt_full_name_contact' => $row->full_name ,
			'txt_email_contact' => 'supermouseno1@gmail.com' , 
			'txt_tel_contact' => $row->tel ,
			'txt_phone_contact' => $row->phone 
		);
		db_set_active('default'); 
		mdInsertRealEstate( $data );
		db_set_active('tibi');
	}	
    
    db_set_active('default'); // We need to call the main (drupal) db back
}

function searchRealestate(){   
	/*-----------get_tring_search---------------------*/
	$url = $_SERVER["REQUEST_URI"];  
	$s = explode('?' , $url);
    $s = $s[1]; 
    /*if (!drupal_lookup_path('alias', 'tim-kiem-nha-dat?' . $s)) {
        $alias_path = array(
            'pid' => NULL,
            'language' => LANGUAGE_NONE,
            'source' => 'tim-kiem-nha-dat?' . $s , 
            'alias' => 'tim-bat-dong-san-1'
        );
       // path_save( $alias_path );
        
        if (isset($alias_path)) {
            $redirect = new stdClass();
            redirect_object_prepare($redirect);
            $redirect->source =  'tim-kiem-nha-dat?' . $s;
            $redirect->redirect = BASE_URL . 'tim-bat-dong-san-1';
           // redirect_save($redirect);
        }
       // drupal_goto( drupal_get_path_alias( 'tim-kiem-nha-dat?' . $s ) );
    // Not a path alias.
    // $path does not exits
    }   
    else{
        
    }*/
	/*------------------------------------------------*/
	
	//add css
    drupal_add_css(drupal_get_path('module', 'tbreal') . '/assets/css/product_realestate.css');    
    drupal_add_css(drupal_get_path('module', 'tbreal') . '/assets/css/filter_product_realestate.css');
    
     //add js
    drupal_add_js(drupal_get_path('module', 'tbreal') . '/assets/js/filter_product_realestate.js');
    drupal_add_js(drupal_get_path('module', 'tbreal') . '/assets/js/lib_realestate.js');
    
    $start = $_GET['page'] > 0 ? ($_GET['page']-1)*20 : 0; 
    
    $result = mdSearchRealestate($_GET , $start , $limit = ITEM_PER_PAGE_20);  
	$data['list_real'] = theme('list_real', array('rows' => $result['data']));       
    $data['block_search_realestate'] = theme('block_search_realestate' , array('s' => $_GET));     
    $data['breadcrumb'] = theme('tbnd_breadcrumb' , array('links' => $result['breadcrumb']));

    $data['pagination'] = theme('pagination' , array('total' => ceil($result['total']/ITEM_PER_PAGE_20) , 'prefix_url' => BASE_URL . 'tim-kiem-nha-dat?' ,  's' => $s , 'page' => $_GET['page'] ? $_GET['page'] : 1));
   
    //$list_statistic_price = mdStatisticProductByPrice( $_POST );
    $data['block_statistic_product_price'] = theme('block_statistic_product_price' , array('title' => $result['title'] , 'list_statistic' => $result['list_statistic_price'] , 'kS' => $result['link_s']));
    $data['block_statistic_product_area'] = theme('block_statistic_product_area' , array('title' => $result['title'] , 'list_statistic' => $result['list_statistic_area']));
    
    $data['title'] = $result['title'];    
    /*--------------add_key_words----------------------*/
    $title = str_replace(',' , '', trim($result['title']));
	addMetaTag('keywords', str_replace(' ' , ', ', trim(api_cut_words($title , 50, $word_cut = '', $charset = 'UTF-8'))));
	/*-------------------------------------------------*/
    /*-----------total_product------------------------------*/
    $data['total'] = $result['total']; 
    $data['start'] = $start;
    /*------------------------------------------------------*/
    
    /*------------remove_after_finish_---------------------*/
    //$list_product_vip = mdGetListRealVipByParam(0 , 10);
    //$data['adv_box_right_300_product_realestate'] = theme('adv_box_right_300_product_realestate' , array('list_vip' => $list_product_vip));
    
    return theme('page_realestate' , $data);
}
